package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppPlaceAreaPopLayoutBinding;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.adapter.transport.TransportLoadGoodsTimeAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.TransportAdapterOnItemClick;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe:    目标地点范围选择弹窗
 * Author:  yangLei
 * Date:    2021/12/17
 */
public class PlaceAreaPopWindow extends BasePopWindow implements View.OnClickListener {

    private List<GoodsInfoTypeEntity3> loadRadius;
    private TransportLoadGoodsTimeAdapter3 areaAdapter1;
    private TransportLoadGoodsTimeAdapter3 areaAdapter2;
    private AppPlaceAreaPopLayoutBinding binding;

    private GoodsInfoTypeEntity3 currDistanceItem1, currDistanceItem2;
    private GoodsInfoTypeEntity3 tempDistanceItem1, tempDistanceItem2;

    public PlaceAreaPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.app_place_area_pop_layout;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        initRadiusData();
        binding = (AppPlaceAreaPopLayoutBinding) holder.getmBinding();
        binding.resetBtn.setOnClickListener(this::onClick);
        binding.confirmBtn.setOnClickListener(this::onClick);

        areaAdapter1 = new TransportLoadGoodsTimeAdapter3(mContext, loadRadius);
        binding.distanceList01.setLayoutManager(new GridLayoutManager(mContext, 4));
        binding.distanceList01.setAdapter(areaAdapter1);
        areaAdapter1.setDefItem(-1);
        areaAdapter1.setItemSelect(true);
        areaAdapter1.setOnItemClick(new TransportAdapterOnItemClick<GoodsInfoTypeEntity3>() {
            @Override
            public void onItemClick(GoodsInfoTypeEntity3 infoTypeEntity, int position) {
                tempDistanceItem1 = infoTypeEntity;
            }
        });

        areaAdapter2 = new TransportLoadGoodsTimeAdapter3(mContext, loadRadius);
        binding.distanceList02.setLayoutManager(new GridLayoutManager(mContext, 4));
        binding.distanceList02.setAdapter(areaAdapter2);
        areaAdapter2.setDefItem(-1);
        areaAdapter2.setItemSelect(true);
        areaAdapter2.setOnItemClick(new TransportAdapterOnItemClick<GoodsInfoTypeEntity3>() {
            @Override
            public void onItemClick(GoodsInfoTypeEntity3 infoTypeEntity, int position) {
                tempDistanceItem2 = infoTypeEntity;
            }
        });

    }

    private void initRadiusData() {
        loadRadius = new ArrayList<>();
        loadRadius.add(new GoodsInfoTypeEntity3("0", "不限"));
        loadRadius.add(new GoodsInfoTypeEntity3("10", "10公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("50", "50公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("100", "100公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("150", "150公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("200", "200公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("250", "250公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("300", "300公里内"));
        loadRadius.add(new GoodsInfoTypeEntity3("500", "500公里内"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset_btn:
                areaAdapter1.resetCheckDatas();
                areaAdapter1.setDefItem(-1);
                currDistanceItem1 = null;
                tempDistanceItem1=null;
                areaAdapter2.resetCheckDatas();
                areaAdapter2.setDefItem(-1);
                currDistanceItem2 = null;
                tempDistanceItem2 = null;
                break;

            case R.id.confirm_btn:
                currDistanceItem1 = tempDistanceItem1;
                currDistanceItem2 = tempDistanceItem2;
                if (onResultClick != null) {
                    onResultClick.result(currDistanceItem1, currDistanceItem2, null);
                }
                dissMiss();
                break;
        }
    }

    /**
     * 获取选中的总共里范围数
     * @return
     */
    public float getSelectTotalKm() {
        float totalNum = 0;
        if ((currDistanceItem1 != null && currDistanceItem1.getNo().equals("0")) || (currDistanceItem2 != null && currDistanceItem2.getNo().equals("0"))) {
            return totalNum;
        }
        if (currDistanceItem1 != null) {
            totalNum += NumberUtil.convertToFloat(currDistanceItem1.getNo());
        }
        if (currDistanceItem2 != null) {
            totalNum += NumberUtil.convertToFloat(currDistanceItem2.getNo());
        }
        return totalNum;
    }

    /**
     * 获取回显的公里数
     * 显示逻辑：两条都选择了具体公里数就显示第一条，否则显示“选择范围”
     *
     * @return
     */
    public float getSelectEchoKm() {
        float kmNum = 0;
        if ((currDistanceItem1 == null || currDistanceItem1.getNo().equals("0")) && currDistanceItem2 != null && !currDistanceItem2.getNo().equals("0")) {
            kmNum = NumberUtil.convertToFloat(currDistanceItem2.getNo());
        } else if (currDistanceItem1 != null && !currDistanceItem1.getNo().equals("0") && (currDistanceItem2 == null || currDistanceItem2.getNo().equals("0"))){
            kmNum = NumberUtil.convertToFloat(currDistanceItem1.getNo());
        } else {
            if (currDistanceItem1 != null) {
                kmNum = NumberUtil.convertToFloat(currDistanceItem1.getNo());
            } else if (currDistanceItem2 != null) {
                kmNum = NumberUtil.convertToFloat(currDistanceItem2.getNo());
            }
        }
        return kmNum;
    }
    /**
     * 获取回显的公里数
     * 显示逻辑：两条都选择了具体公里数就显示第一条，否则显示“选择范围”
     *
     * @return  距离目的地范围公里数
     */

    public float getSelectEchoKm02(){
        if (currDistanceItem1 != null&&!currDistanceItem1.getNo().equals("0")&&currDistanceItem2!=null&&!currDistanceItem2.getNo().equals("0")){
            return NumberUtil.convertToFloat(currDistanceItem1.getNo());
        }else {
            return 0f;
        }
    }

}
